package com.aegean.MonitorExamSYS.tool.dao.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.aegean.MonitorExamSYS.monitor.dao.MonitorDao;
import com.aegean.MonitorExamSYS.tool.dao.ToolDao;
import com.aegean.MonitorExaxSYS.model.BigType;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.SmallType;

@Repository
@Transactional
public class ToolDaoImpl implements ToolDao {
	@Resource
	SessionFactory factory;

	@Override
	public int searchBigTypeCount() {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			cr.add(Restrictions.eq("big_State", 1));
			cr.setCacheable(true);
			int count = cr.list().size();
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<BigType> searchPartBigTypes(int start, int limit) {
		Session session = null;
		List<BigType> bigtypes = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			cr.add(Restrictions.eq("big_State", 1));
			cr.setFirstResult(start);
			cr.setMaxResults(limit);
			cr.setCacheable(true);
			bigtypes = (List<BigType>) cr.list();
			return bigtypes;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int searchSmallTypeCount(int big_Id) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(SmallType.class);
			cr.add(Restrictions.eq("small_Big.big_Id", big_Id));
			cr.add(Restrictions.eq("small_State", 1));
			cr.setCacheable(true);
			return cr.list().size();
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<SmallType> searchPartSmallTypes(int big_Id, int start, int limit) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(SmallType.class);
			cr.add(Restrictions.eq("small_Big.big_Id", big_Id));
			cr.add(Restrictions.eq("small_State", 1));
			cr.setCacheable(true);
			return cr.list();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public ExamInfo searchExamInfo(int exam_Id) {
		Session session;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(ExamInfo.class);
			cr.add(Restrictions.eq("examInfo_Id", exam_Id));
			cr.setCacheable(true);
			return (ExamInfo) cr.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<ExamInfo> showExamList(String key, int small_Id,
			int exam_status, int start, int limit, String exam_Name,
			Date startTime, Date endTime, int status, DetachedCriteria dr) {
		// TODO Auto-generated method stub
		Session session;
		List<ExamInfo> examinees;
		try {
			session = factory.getCurrentSession();
			switch (status) {
			case 1:// 查询最新考试动态列显最多8个
				Criteria c = dr.getExecutableCriteria(session);
				c.setCacheable(true);
				c.setFirstResult(start);
				c.setMaxResults(limit);
				examinees = c.list();
				return examinees;
			case 2:// 模糊查询考试信息
				String hql;
				if ((key == null || key.trim().equals("")) && small_Id == 0
						&& exam_status == -1) {
					hql = "from ExamInfo ";
				} else {
					hql = "from ExamInfo where ";
				}
				if (key != null && !key.trim().equals("")) {
					hql += "examInfo_Name like '" + key + "' and ";
				}
				if (small_Id != 0) {
					hql += "examInfo_Type.small_Id=" + small_Id + " and ";
				}
				if (exam_status == 1 || exam_status == 4) {
					hql += "examInfo_State=" + (exam_status - 1);
				}
				if (hql.endsWith(" and ")) {
					hql = hql.substring(0, hql.length() - 5).trim();
				}
				hql += " order by examInfo_StartTime desc";
				Query q = session.createQuery(hql);
				q.setCacheable(true);
				q.setFirstResult(start);
				q.setMaxResults(limit);
				examinees = q.list();
				return examinees;
			case 3:
				break;
			case 4:
				break;
			default:
				break;
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int countExamList(String key, int small_Id, int exam_status,
			int start, int limit, String exam_Name, Date startTime,
			Date endTime, int status, DetachedCriteria dr) {
		// TODO Auto-generated method stub
		Session session;
		List<ExamInfo> examinees;
		try {
			session = factory.getCurrentSession();
			switch (status) {
			case 1:// 查询最新考试动态列显最多8个
				Criteria c = dr.getExecutableCriteria(session);
				c.setCacheable(true);
				examinees = c.list();
				return examinees.size();
			case 2:// 模糊查询考试信息
				String hql;
				if ((key == null || key.trim().equals("")) && small_Id == 0
						&& exam_status == -1) {
					hql = "from ExamInfo ";
				} else {
					hql = "from ExamInfo where ";
				}

				if (key != null && !key.trim().equals("")) {
					hql += "examInfo_Name like '" + key + "' and ";
				}
				if (small_Id != 0) {
					hql += "examInfo_Type.small_Id=" + small_Id + " and ";
				}
				if (exam_status == 1 || exam_status == 4) {
					hql += "examInfo_State=" + (exam_status - 1);
				}
				if (hql.endsWith(" and ")) {
					hql = hql.substring(0, hql.length() - 5).trim();
				}
				hql += " order by examInfo_StartTime desc";
				Query q = session.createQuery(hql);
				q.setCacheable(true);
				examinees = q.list();
				return examinees.size();
			case 3:
				break;
			case 4:
				break;
			default:
				break;
			}
			return 0;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

}